const { order } = require("../../../api.js");

var e = require("../../../utils/function.js"), t = getApp(), r = require("../../../api.js");
var initialization
Page({
    data: {
        img: e.driverImg,
        time: 10,
        order: [],
        latitude: "",
        longitude: "",
        speed: "",
        accuracy: "",
        is_stop: 0,
        apart:'',
        estimated_form_time: '',
         imgs: e.userImg,
        get_order_timer: "",
        order_url: [ "/pages/driverOrder/orderedStatus/orderedStatus", "/pages/driverOrder/orderGoing/orderGoing", "/pages/driverOrder/bill/bill", "/pages/driverOrder/orderDetail/orderDetail", "/pages/driverOrder/orderedStatus/orderedStatus" ]
    },
    onShow: function(e) {
        console.log('显示');
        wx.getStorageSync("like")
      initialization = setInterval(()=>{
            this.getone(wx.getStorageSync("like"));
        },5000)
         
    },
    onHide: function(e){
        console.log('隐藏');
            // clearInterval(initialization)
    },
    onLoad: function(e) {
        e.like && (console.log(e.like), t.like = e.like), e.uid && t.shareCouponsMessage(e.uid);
    },
    refuse: function() {
        var e = this;
        wx.showModal({
            title: "提示",
            content: "是否拒绝此单",
            success: function(a) {
                a.confirm ? t.request({
                    url: r.order.refuseOrder,
                    method: "post",
                    data: {
                        order_no: e.data.order[0].order_no
                    },
                    success: function(t) {
                        t.msg == '拒绝成功'  && e.setData({
                            order: []
                        })
                        clearInterval(e.data.get_order_timer), e.setData({
                            time: 0,
                        }), wx.showToast({
                          title: t.msg ,
                          icon:'none'
                        }), e.getone(wx.getStorageSync("like"));
                        
                    }
                }) : a.cancel;
            }
        });
    },
    setorder: function() {},
    getone: function(e) {
        var a = this;
        console.log(e), wx.getLocation({
            type: "gcj02",
            success: function(o) {
                t.request({
                    url: r.order.getOrder,
                    method: "post",
                    data: {
                        latitude: o.latitude,
                        longitude: o.longitude,
                        speed: o.speed,
                        accuracy: o.accuracy,
                        like: e
                    },
                    success: function(e) {
                    
                        var  data  = e.data
                       
                           
                      if(data != '' ) {
                        a.setData({
                            apart: e.data[0].apart,
                            estimated_form_time:e.data[0]. estimated_form_time
                        })
                      }
                       
                        var p = 'https://dishichuxing.cn'
                        e.data[0] &&  t.musicPlay( p + e.data[0].voice_announcements)
                        
                        wx.hideLoading(), clearInterval(a.data.get_order_timer), 0 == e.code ? (wx.showToast({
                            title: e.msg,
                            icon: "none"
                        }), wx.reLaunch({
                            url: "../../index/index"
                        })) : (wx.setStorageSync("is_stop", 0), a.setData({
                            order: e.data
                        }), a.data.get_order_timer = setInterval(function() {
                            0 == a.data.time ? (a.getone(wx.getStorageSync("like")), a.setData({
                                time: 10
                            })) : a.setData({
                                time: a.data.time - 1
                            });
                        }, 1e3));
                    }
                });
            }
        });
    },
    timeTsk: function() {
        var e = this, t = setInterval(function() {
            e.data.time > 0 ? e.setData({
                time: --e.data.time
            }) : 1 == wx.getStorageSync("is_stop") ? clearTimeout(t) : (e.getone(), e.setData({
                time: 10
            }));
        }, 1e3);
    },
    onUnload: function() {
        console.log('执行');
        clearInterval(initialization)
        clearInterval(this.data.get_order_timer);
    },
    intoPageHandler: function(a) {
        clearInterval(this.data.get_order_timer);
        var o = this;
        if (o.data.order.length) {
            var s = o.data.order[0].order_no;
            t.uploadLocation(s);
            var d = "../orderedStatus/orderedStatus?order_no=" + s;
            wx.showLoading({
                title: ""
            }), t.request({
                url: r.order.grabsOrder,
                method: "post",
                data: {
                    order_no: s,
                    formid: null,
                    origin: 0
                },
                success: function(a) {
                    console.log(r.message.getTmplIds)
                    wx.hideLoading(), wx.setStorageSync("is_stop", 1), 1 == a.code ? (t.musicPlay("https://driving.4hl.cn/WeChat/resource/video/get_order.mp3"), 
                    wx.setStorageSync("current_order", this.data.order), t.gateway(s, 1), wx.canIUse("requestSubscribeMessage") && t.request({
                        url: r.message.getTmplIds,
                        method: "GET",
                        data: {
                            type: 2
                        },
                        success: function(e) {
                            wx.requestSubscribeMessage({
                                tmplIds: e.data,
                                complete: function(e) {
                                    console.log(e);
                                }
                            });
                        }
                    }), 1 == o.data.order[0].is_pre ? e.intoPageHandler(d) : wx.redirectTo({
                        url: "/pages/driverOrder/order/order"
                    })) : (wx.showToast({
                        title: a.msg,
                        duration: 1e3,
                        icon: "none"
                    }), 1001 == a.code && setTimeout(function() {
                        if (4 == a.data.is_service) return e.intoPageHandler(o.data.order_url[a.data.is_service] + "?orderstart=1&order_no=" + a.data.order_no), 
                        !1;
                        e.intoPageHandler(o.data.order_url[a.data.is_service] + "?order_no=" + a.data.order_no);
                    }, 800));
                }
            });
        } else wx.showToast({
            title: "订单不存在",
            icon: "none"
        });
    },
    onShareAppMessage: function() {
        var e = t.shareAppMessage().title, r = t.shareAppMessage().imageUrl, a = t.shareAppMessage().desc, o = t.shareAppMessage().path;
        return console.log(t.shareAppMessage()), {
            title: e,
            imageUrl: r,
            desc: a,
            path: o
        };
    }
});